package com.novell.sasl.client;

import org.apache.harmony.javax.security.sasl.SaslException;

class TokenParser
{
  private static final int STATE_DONE = 6;
  private static final int STATE_LOOKING_FOR_COMMA = 4;
  private static final int STATE_LOOKING_FOR_FIRST_TOKEN = 1;
  private static final int STATE_LOOKING_FOR_TOKEN = 2;
  private static final int STATE_PARSING_ERROR = 5;
  private static final int STATE_SCANNING_TOKEN = 3;
  private int m_curPos;
  private int m_scanStart;
  private int m_state;
  private String m_tokens;

  TokenParser(String paramString)
  {
    this.m_tokens = paramString;
    this.m_curPos = 0;
    this.m_scanStart = 0;
    this.m_state = 1;
  }

  boolean isValidTokenChar(char paramChar)
  {
    int i;
    if (((paramChar >= 0) && (paramChar <= ' ')) || ((paramChar >= ':') && (paramChar <= '@')) || ((paramChar >= '[') && (paramChar <= ']')) || (',' == paramChar) || ('%' == paramChar) || ('(' == paramChar) || (')' == paramChar) || ('{' == paramChar) || ('}' == paramChar) || ('' == paramChar))
      i = 0;
    while (true)
    {
      return i;
      i = 1;
    }
  }

  boolean isWhiteSpace(char paramChar)
  {
    int i;
    if (('\t' == paramChar) || ('\n' == paramChar) || ('\r' == paramChar) || (' ' == paramChar))
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  String parseToken()
    throws SaslException
  {
    String str2;
    char c;
    String str1 = null;
    if (this.m_state == 6)
    {
      str2 = null;
      return str2;
    }
    do
    {
      c = this.m_tokens.charAt(this.m_curPos);
      switch (this.m_state)
      {
      default:
        this.m_curPos = (1 + this.m_curPos);
      case 1:
      case 2:
      case 3:
      case 4:
      }
    }
    while ((this.m_curPos < this.m_tokens.length()) && (str1 == null));
    if (str1 == null);
    switch (this.m_state)
    {
    case 1:
    case 4:
    default:
    case 3:
      while (true)
      {
        while (true)
        {
          do
          {
            while (true)
            {
              while (true)
              {
                do
                {
                  while (true)
                  {
                    do
                      while (true)
                        str2 = str1;
                    while (isWhiteSpace(c));
                    if (!(isValidTokenChar(c)))
                      break;
                    this.m_scanStart = this.m_curPos;
                    this.m_state = 3;
                  }
                  this.m_state = 5;
                  throw new SaslException("Invalid token character at position " + this.m_curPos);
                }
                while (isValidTokenChar(c));
                if (!(isWhiteSpace(c)))
                  break;
                str1 = this.m_tokens.substring(this.m_scanStart, this.m_curPos);
                this.m_state = 4;
              }
              if (',' != c)
                break;
              str1 = this.m_tokens.substring(this.m_scanStart, this.m_curPos);
              this.m_state = 2;
            }
            this.m_state = 5;
            throw new SaslException("Invalid token character at position " + this.m_curPos);
          }
          while (isWhiteSpace(c));
          if (c != ',')
            break;
          this.m_state = 2;
        }
        this.m_state = 5;
        throw new SaslException("Expected a comma, found '" + c + "' at postion " + this.m_curPos);
        str1 = this.m_tokens.substring(this.m_scanStart);
        this.m_state = 6;
      }
    case 2:
    }
    throw new SaslException("Trialing comma");
  }
}